<?php


$searchString = "";
$ErrorCode = "-1";

if(!empty($_POST["PriceFrom"]))
{	
	if(is_numeric($_POST["PriceFrom"]))
	{
		$searchString .= " WHERE p.ASKING_PRICE >= " . $_POST["PriceFrom"];
	}else{
		$ErrorCode = "1";
	}
}

if(!empty($_POST["PriceTo"]) && !$_POST["PriceTo"] == "")
{
	if(is_numeric($_POST["PriceTo"]))
	{
		$searchString .=  AndOrWhere($searchString) . " p.ASKING_PRICE <= " . $_POST["PriceTo"];	
	}else{
		$ErrorCode = "2";
	}
}

if(!empty($_POST["DateListed"]) && !$_POST["DateListed"] == "")
{
	switch($_POST["DateListed"])
	{
		case 1:
		{
			$searchString .=  AndOrWhere($searchString) ."  l.LISTING_DATE >= DATE_SUB(NOW(),INTERVAL 1 DAY) ";
			break;
		}

		case 2:
		{
			$searchString .=  AndOrWhere($searchString) ."  l.LISTING_DATE >= DATE_SUB(NOW(),INTERVAL 7 DAY) ";
			break;
		}

		case 3:
		{
			$searchString .=  AndOrWhere($searchString) ."  l.LISTING_DATE >= DATE_SUB(NOW(),INTERVAL 1 MONTH) ";
			break;
		}

		case 4:
		{
			$searchString .=  AndOrWhere($searchString) ."  l.LISTING_DATE >= DATE_SUB(NOW(),INTERVAL 3 MONTH) ";
			break;
		}

		case 5:
		{
			$searchString .=  AndOrWhere($searchString) ."  l.LISTING_DATE >= DATE_SUB(NOW(),INTERVAL 12 MONTH) ";
			break;
		}
	}
	
}

if(isset($_POST["SearchType"]) && $_POST["SearchType"] == "Advanced")
{
	if(!empty($_POST["LotSizeFrom"]))
	{
		if(is_numeric($_POST["LotSizeFrom"]))
		{
			$searchString .= AndOrWhere($searchString) ." p.LOT_SIZE >= " . $_POST["LotSizeFrom"];	
		}else{
			$ErrorCode = "3";
		}
	}

	if(isset($_POST["LotSizeTo"]) && !$_POST["LotSizeTo"] == "")
	{
		if(is_numeric($_POST["LotSizeTo"]))
		{
			$searchString .= AndOrWhere($searchString) ." p.LOT_SIZE <= " . $_POST["LotSizeTo"];
		}else{
			$ErrorCode = "4";
		}
			
	}

	if(isset($_POST["YearBuiltFrom"]) && !$_POST["YearBuiltFrom"] == "")
	{
		if(is_numeric($_POST["YearBuiltFrom"]) && strlen($_POST["YearBuiltFrom"]) == 4)
		{
			$searchString .= AndOrWhere($searchString) ." p.YEAR_BUILT >= " . $_POST["YearBuiltFrom"];		
		}else{
			$ErrorCode = "5";
		}
	}
	
	if(isset($_POST["YearBuiltTo"]) &&!$_POST["YearBuiltTo"] == "")
	{
		if(is_numeric($_POST["YearBuiltTo"]) && strlen($_POST["YearBuiltTo"]) == 4)
		{
			$searchString .= AndOrWhere($searchString) ." p.YEAR_BUILT <= " . $_POST["YearBuiltTo"];	
		}else{
			$ErrorCode = "6";
		}
	}
	
	if(isset($_POST["PropertyType"]) && !$_POST["PropertyType"] == "" && $_POST["PropertyType"] != "-1")
	{
		$searchString .=  AndOrWhere($searchString) ." p.PROPERTY_TYPE = '" . $_POST["PropertyType"] ."' ";
	}
	
	if(isset($_POST["CoolingType"]) && !$_POST["CoolingType"] == "" && $_POST["CoolingType"] != "-1")
	{
		$searchString .=  AndOrWhere($searchString) ." p.COOLING_TYPE = '" . $_POST["CoolingType"] ."' ";
	}
	
	if(isset($_POST["HeatingType"]) && !$_POST["HeatingType"] == "" && $_POST["HeatingType"] != "-1")
	{
		$searchString .=  AndOrWhere($searchString) ." p.HEATING_TYPE = '" . $_POST["HeatingType"] ."' ";
	}
	
	if(isset($_POST["Garage"]) && $_POST["Garage"] == "on")
	{
		$searchString .=  AndOrWhere($searchString) ." p.GARAGE > 0 ";
	}
	
	if(isset($_POST["Parking"]) && $_POST["Parking"] == "on")
	{
		$searchString .=  AndOrWhere($searchString) ." p.PARKING > 0 ";
	}
	
	if(isset($_POST["Driveway"]) && $_POST["Driveway"] == "on")
	{
		$searchString .=  AndOrWhere($searchString) ." p.DRIVEWAY > 0 ";
	}
		
	if(isset($_POST["ZoningType"]) && $_POST["ZoningType"] != "" && $_POST["ZoningType"] != "-1")
	{
		$searchString .=  AndOrWhere($searchString) ." p.ZONING = '" . $_POST["ZoningType"] ."' ";
	}
}

if(empty($searchString))
{
	$searchString .= " WHERE DATEDIFF(NOW(),l.EXPIRY_DATE) <= 0 ";
}else{
	$searchString .= " AND DATEDIFF(NOW(),l.EXPIRY_DATE) <= 0 ";
}	

if(empty($_POST["RoomCount"]) || !$_POST["RoomCount"] || $_POST["RoomCount"] == -1)
{
}else{

	if($_POST["RoomCount"] == "8+")
	{
		$searchString .= " HAVING ROOMCOUNT >= 8 ";
	}else{
		$searchString .= " HAVING ROOMCOUNT = " . $_POST["RoomCount"] . " ";
	}
}

$searchString .= " ORDER BY l.LISTING_DATE DESC ";


//echo "<br /><br />ERROR CODE : " . $ErrorCode;



if($ErrorCode != "-1")
{
	echo '<div class="error">One or more search values are invalid</div>';
}
//	INITIATE DATABASE CONNECTION
include('connect.php');
	
	if($ErrorCode == -1)
	{
		if(empty($searchString))
		{
			$result = mysql_query("SELECT *   FROM LISTS l INNER JOIN PROPERTY p on p.PRTID = l.PRTID   LEFT JOIN (select prtid ,count(*) as ROOMCOUNT from ROOMS group by prtid ) r on r.prtid=p.prtid WHERE DATEDIFF(NOW(),l.EXPIRY_DATE) <= 0  ORDER BY l.LISTING_DATE DESC");
		}else{
			$result = mysql_query("SELECT *   FROM LISTS l INNER JOIN PROPERTY p on p.PRTID = l.PRTID   LEFT JOIN (select prtid ,count(*) as ROOMCOUNT from ROOMS group by prtid ) r on r.prtid=p.prtid" . $searchString );
		}
	
		if(!$result)
		{
			echo '<br />ERROR535 ';
			die('SQL Command Error: ' . mysql_error());
		}
	}

//BASIC SEARCH

if(empty($_POST["SearchType"]) || $_POST["SearchType"] != "Advanced")
{
echo ('	<form name="search" id="search" method="POST" action="index.php?page=search">
		<TABLE border=2>
		<TR><TD colspan=10 class="center">Basic Search</TD></TR>
		<TR>
		<TD>Price Range</TD>
		<TD>From <input type="text" name="PriceFrom" id="PriceFrom" value="' . getorempty("PriceFrom").'" '. ($ErrorCode == "1" ? "class=\"error\"":"").' /></TD> 
		<TD>To <input type="text" name="PriceTo" id="PriceTo" value="'. getorempty("PriceTo") .'" '. ($ErrorCode == "2" ? "class=\"error\"":"").' /></TD>
		</TR>
		<TR><TD>Rooms</TD><TD colspan=2>'. create_combobox("SEARCH_ROOMCOUNT","RoomCount",true,getorempty("RoomCount")) .'</TD></TR>
		<TR><TD>Date Listed</TD><TD colspan=2>'. create_combobox("SEARCH_DATELISTED","DateListed",false,getorempty("DateListed")).'</TD></TR>
		<TR><TD colspan=10 class="right"><input type="text" name="SearchType" id="SearchType" value="Basic" hidden="true"><a href="#" onclick="document.search.SearchType.value=\'Advanced\';document.search.submit();">Advanced Search</a><input type="submit" value="Search"/></TD></TR>
		</TABLE>
		</form>
');
}else{
echo ('	<form name="search" id="search" method="POST" action="index.php?page=search">
		<TABLE border=2>
		<TR><TD colspan=10 class="center">Advanced Search</TD></TR>
		<TR>
		<TD>Price Range</TD>
		<TD>From <input type="text" name="PriceFrom" id="PriceFrom" value="'. getorempty("PriceFrom") .'" '. ($ErrorCode == "1" ? "class=\"error\"":"").' /></TD> 
		<TD>To <input type="text" name="PriceTo" id="PriceTo" value="'. getorempty("PriceTo") .'" '. ($ErrorCode == "2" ? "class=\"error\"":"").' /></TD> 
		</TR>
		<TR>
		<TD>Lot Size (ft/sq)</TD><TD>From <input type="text" name="LotSizeFrom" id="LotSizeFrom" value="'. getorempty("LotSizeFrom") .'" '. ($ErrorCode == "3" ? "class=\"error\"":"").'/></TD> 
		<TD>To <input type="text" name="LotSizeTo" id="LotSizeTo" value="'. getorempty("LotSizeTo") .'" '. ($ErrorCode == "4" ? "class=\"error\"":"").'/> </TD>
		</TR>
		<TR>
		<TD>Year Built</TD>
		<TD>From <input type="text" name="YearBuiltFrom" id="YearBuiltFrom" value="'. getorempty("YearBuiltFrom") .'" '. ($ErrorCode == "5" ? "class=\"error\"":"").'/></TD>
		<TD>To <input type="text" name="YearBuiltTo" id="YearBuiltTo" value="'. getorempty("YearBuiltTo") .'" '. ($ErrorCode == "6" ? "class=\"error\"":"").'/> </TD>
		</TR>
		<TR><TD>Rooms</TD><TD colspan=2>'. create_combobox("SEARCH_ROOMCOUNT","RoomCount",true,getorempty("RoomCount")) .'</TD></TR>
		<TR><TD>Date Listed</TD><TD colspan=2>'. create_combobox("SEARCH_DATELISTED","DateListed",false,getorempty("DateListed")).'</TD></TR>
		<TR><TD>Property Type</TD><TD colspan=2>'. create_combobox("PROPERTY_TYPE","PropertyType",true,getorempty("PropertyType")).'</TD></TR>
		<TR><TD>Cooling Type</TD><TD colspan=2>'. create_combobox("COOLING_TYPE","CoolingType",true,getorempty("CoolingType")).'</TD></TR>
		<TR><TD>Heating Type</TD><TD colspan=2>'. create_combobox("HEATING_TYPE","HeatingType",true,getorempty("HeatingType")).'</TD></TR>
		<TR><TD>Garage</TD><TD colspan=2><input type="checkbox" name="Garage" id="Garage" '.(getorempty("Garage") == "on" ? "checked=\"true\"":"") .'"></TD></TR>
		<TR><TD>Parking</TD><TD colspan=2><input type="checkbox" name="Parking" id="Parking" '.(getorempty("Parking") == "on" ? "checked=\"true\"":"") .'"></TD></TR>
		<TR><TD>Driveway</TD><TD colspan=2><input type="checkbox" name="Driveway" id="Driveway" '.(getorempty("Driveway") == "on" ? "checked=\"true\"":"") .'"></TD></TR>
		<TR><TD>Zoning</TD><TD colspan=2>'. create_combobox("ZONING_TYPE","ZoningType",true,getorempty("ZoningType")).'</TD></TR>
		<TR><TD colspan=10 class="right"><input type="text" name="SearchType" id="SearchType" value="Advanced" hidden="true"><a href="#" onclick="document.search.SearchType.value =\'Basic\';document.search.submit();">Basic Search</a><input type="submit" value="Search"/></TD></TR>
		</TABLE>
		</form>
');
}

echo '<br /><br />Search Results<hr>';
//Search RESULT
echo ('	
<TABLE border=2>
<form name="Result" id="Result" method="POST" action="index.php?page=property-view">
<input type="text" name="PRTID" id="PRTID" value="" hidden="true"/>
	<TR>
		<TH>View</TH>
		<TH>Property Type</TH>
		<TH>Asking Price</TH>
		<TH>Evaluated Price</TH>
		<TH>Year Built</TH>
		<TH>Taxes</TH>
		<TH>Utility Cost</TH>
		<TH>Driveway</TH>
		<TH>Garage</TH>
		<TH>Parking</TH>
		<TH>Zoning</TH>
		<TH>Heating</TH>
		<TH>Cooling</TH>
		<TH>Lot Size</TH>
	</TR>
');

	if($ErrorCode == -1)
	{
		while($row = mysql_fetch_assoc($result))
		{
		  echo('  		
				  <TR>
				  <TD class="center"><a href="#" onclick="document.Result.PRTID.value=\''. $row['PRTID'] .'\' ;document.Result.submit();">View Property</a></TD>
				  <TD class="center">' . $row['PROPERTY_TYPE'] . '</TD>
				  <TD class="center">' . $row['ASKING_PRICE'] . '$</TD>
				  <TD class="center">' . $row['EVALUATION'] . '$</TD>
				  <TD class="center">' . $row['YEAR_BUILT'] .'</TD>
				  <TD class="center">' . $row['TAXES'] . '$</TD>
				  <TD class="center">' . $row['UTILITY_COSTS'] . '$</TD>
				  <TD class="center">' . $row['DRIVEWAY'] .' Places</TD>
				  <TD class="center">' . $row['GARAGE'] .' Places</TD>
				  <TD class="center">' . $row['PARKING'] .' Places</TD>
				  <TD class="center">' . $row['ZONING'] . '</TD>
				  <TD class="center">' . $row['HEATING_TYPE'] . '</TD>
				  <TD class="center">' . $row['COOLING_TYPE'] . '</TD>
				  <TD class="center">' . $row['LOT_SIZE'] . ' Ft/sq</TD>
				  </TR>
				  
		  ');
		}
		echo' </FORM>';
  }
echo '</TABLE>';

//	CLOSE DATABASE CONNECTION
include('disconnect.php');

function getorempty($var)
{
	if (empty($_POST[$var])) return "";
	return trim($_POST[$var]);
}
function getorstar($var)
{
	if (empty($_POST[$var])) return "";
	return trim($_POST[$var]);
}


?> 
